package compilador.analisadorSintatico.cfg;

import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

/**
 *
 * @author Henrique
 */
public class AnalisadorSintaticoProperties {
    private static final String BUNDLE_NAME = "compilador.analisadorSintatico.cfg.AnalisadorSintaticoProperties";

    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
                    .getBundle(BUNDLE_NAME);

    private AnalisadorSintaticoProperties() {
    }

    public static String getPropertie(String key) {
            try {
                    return RESOURCE_BUNDLE.getString(key);                        
            } catch (MissingResourceException e) {
                    return null;
            }
    }
        
    public static String[] getPropertiesArray(String key) {
        String[] array =  null;
        try {  
            String resource = RESOURCE_BUNDLE.getString(key);
            StringTokenizer st = new StringTokenizer(resource, ",");
            int tokens = st.countTokens();  
            array = new String[tokens];

            for (int i = 0; i < tokens; i++) {  
                array[i] = st.nextToken().trim();
            }

        } catch (Exception e) {  
            System.err.print(e.getMessage());              
        }  
        return array;
    }

    /*
     * key = [resource,data]
     */
    public static String[][] getPropertiesMatriz(String key) {
        String[][] array =  null;
        try {
            String resource = RESOURCE_BUNDLE.getString(key);
            
            resource = resource.replaceAll("']'", "#");
            
            resource = resource.substring(0, resource.lastIndexOf(']')); //removendo espaços após o ']'

            StringTokenizer st = new StringTokenizer(resource, "]");
            int tokens = st.countTokens();
            array = new String[tokens][2];
            
            for (int i = 0; i < tokens; i++) {
                String token = st.nextToken();
                
                token = token.replaceAll("#", "]");
                
                int index = token.indexOf(',');
                array[i][0] = token.substring(token.indexOf('[') + 1,index).trim();
                array[i][1] = token.substring(index + 1).trim();
            }

        } catch (Exception e) {
            System.err.print(e.getMessage());              
        }
        return array;
    }

    public static void main(String args[]){
        System.out.println(AnalisadorSintaticoProperties.getPropertie("VALOR_NUM.SEG"));
        
        String[][] propertiesMatriz = AnalisadorSintaticoProperties.getPropertiesMatriz("VALOR_NUM.SEG");
        for(int i = 0; i < propertiesMatriz.length; i++)
            System.out.println(i + " " + propertiesMatriz[i][0] + " # " + propertiesMatriz[i][1]);
    }
}
